package com.xy.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;

public class FileUtil {
    private static final Integer BUFFER_LENGTH = 4096;

    public static String uploadTemplateFile(MultipartFile file, String filePath, String fileName) {
        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        int n;
        File f = new File(filePath + fileName);

        try {
            InputStream is = file.getInputStream();
            OutputStream os = new FileOutputStream(f);
            byte[] buffer = new byte[4096];
            while ((n = is.read(buffer,0, BUFFER_LENGTH)) != -1){
                os.write(buffer,0, n);
            }
            // 读取文件第一行
            BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
            // 输出路径
            bufferedReader.close();
            os.close();
            is.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return filePath + fileName;
    }

}
